package com.hs.test_push2_server.test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;

public class FileUploader {

    public static void main(String[] args) {
        String url = "http://10.120.201.10:8090/ajax/loadAndParseXmlFlie";// 替换为实际的目标URL
        String filePath = "D://Results_20240525024415.xml"; // 替换为实际的文件路径
        File file = new File(filePath);
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            System.out.println(file.getName());
            builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName());

            HttpEntity multipart = builder.build();
            httpPost.setEntity(multipart);

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();

            if (responseEntity != null) {
                String responseString = EntityUtils.toString(responseEntity);
                System.out.println("Response: " + responseString);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}